<?php
define("THIS_PAGE", "products");
require_once($_SERVER["DOCUMENT_ROOT"] . "/../sys/libs/init.lib");
include($_SERVER["DOCUMENT_ROOT"] . "/../sys/includes/adminheader.php");

if (empty($_REQUEST["do"])) {
	header("Location: ?do=index");
}

$do = strtolower(trim($_REQUEST["do"]));

print "<div class=\"pagebody\">";

if ($do == "index") {

} else if ($do == "newproduct") {

	if (!isset($_REQUEST["submit"]) || strtolower($_REQUEST["submit"]) != "add") {

		$form = new formBuilder("NewProducts", array("action" => $_SERVER["PHP_SELF"], "method" => "post"));

		print 	"<h1>New Product</h1><table width=\"600\">
				<tr>
					<td>Product Category</td>
					<td><select name=\"frm_itemcategoryid\">";

		$cats = $db->getarray($queries->itemCatSQL());
		foreach($cats as $key => $cat) {
			print "<option value=\"{$cat->id}\">{$cat->Name}</option>";
		}

		print 	"</select></tr>
				<tr>
					<td>Name</td>
					<td>" . $form->hidden(array("name" => "do", "value" => "newproduct")) . $form->input(array("name" => "frm_name")) . "</td>
				</tr>
				<tr>
					<td>Default Description</td>
					<td>" . $form->input(array("name" => "frm_defaultdescription")) . "</td>
				</tr>
				<tr>
					<td>Amount</td>
					<td>" . $form->input(array("name" => "frm_amount")) . "</td>
				</tr>
				<tr>
					<td>Tax Amount (%)</td>
					<td>" . $form->input(array("name" => "frm_taxamount")) . "</td>
				</tr>
				<tr>
					<td></td>
					<td>" . $form->submit(array("name" => "submit", "value" => "add")) . "</td>
				</tr>
				</table></form>";

	} else if (strtolower($_REQUEST["submit"]) == "add") {

		$itemsavedata["Name"] = $_REQUEST["frm_name"];
		$itemsavedata["DefaultDescription"] = $_REQUEST["frm_defaultdescription"];
		$itemsavedata["Amount"] = $_REQUEST["frm_amount"];
		$itemsavedata["TaxAmount"] = $_REQUEST["frm_taxamount"];

		if ($itemid = $db->insert("items", $itemsavedata)) {
			if ($db->insert("item_itemcategory", array("ItemID" => $itemid, "ItemCategoryID" => $_REQUEST["frm_itemcategoryid"]))) {
				print "<h1>New Product</h1>Product Added Successfully";
			}
		}

	}

} else if ($do == "newcategory") {

	if (!isset($_REQUEST["submit"]) || strtolower($_REQUEST["submit"]) != "add") {

		$form = new formBuilder("NewCategory", array("action" => $_SERVER["PHP_SELF"], "method" => "post"));

		print 	"<h1>New Category</h1><table width=\"600\">
				<tr>
					<td>Name</td>
					<td>" . $form->hidden(array("name" => "do", "value" => "newcategory")) . $form->input(array("name" => "frm_name", "class" => "forminput")) . "</td>
				</tr>
				<tr>
					<td></td>
					<td>" . $form->submit(array("name" => "submit", "value" => "add")) . "</td>
				</tr>
				</table></form>";

	} else if (strtolower($_REQUEST["submit"]) == "add") {

		$savedata["name"] = $_REQUEST["frm_name"];
		if ($db->insert("itemcategory", $savedata)) {
			print "<h1>New Category</h1>Category Added";
		}

	}

} else if ($do == "list") {

	$products = $db->getarray($queries->productsList());

	$form = new formBuilder;
	print "<table width=\"100%\" border=\"0\" cellspacing=\"0\">\n
		   <tr>\n
				\t<th align=\"left\">Name</th>\n
				\t<th align=\"left\">Description</th>\n
				\t<th align=\"right\">Amount</th>\n
				\t<th align=\"right\">Tax</th>\n
				\t<th align=\"right\">E X</th>\n
		   </tr>\n";
	
	$row1 = "#ffffff";
	$row2 = "#9999ff";

	$currentcat = "";
	$i = 0;
	foreach ($products as $key => $product) {

		if ($currentcat != $product->CategoryID) {
			$bgcolor = ($i++ % 2) ? $row1 : $row2;
			print "<tr style=\"background-color:{$bgcolor};\">\n
					\t<td><strong>{$product->CategoryName}</strong></td>\n
					\t<td></td>\n
					\t<td></td>\n
					\t<td></td>\n
					\t<td align=\"right\">" . $form->checkbox(array("name" => "editcat", "value" => $product->CategoryID, "title" => "Edit " . $product->CategoryName, "alt" => "Edit " . $product->CategoryName)) . " " . $form->checkbox(array("name" => "deletecat", "value" => $product->CategoryID, "title" => "Delete " . $product->CategoryName, "alt" => "Delete " . $product->CategoryName)) . "</td>\n
				  </tr>\n";
		}

		$bgcolor = ($i++ % 2) ? $row1 : $row2;

		print "<tr style=\"background-color:{$bgcolor};\">\n
				\t<td style=\"padding-left: 20px;\">{$product->Name}</td>\n
				\t<td>{$product->DefaultDescription}</td>\n
				\t<td align=\"right\">\${$product->Amount}</td>\n
				\t<td align=\"right\">{$product->TaxAmount}%</td>\n
				\t<td align=\"right\">" . $form->checkbox(array("name" => "editprod", "value" => $product->id, "title" => "Edit " . $product->Name, "alt" => "Edit " . $product->Name)) . " " . $form->checkbox(array("name" => "deleteprod", "value" => $product->id, "title" => "Delete " . $product->Name, "alt" => "Delete " . $product->Name)) . "</td>
			  </tr>\n";

		$currentcat = $product->CategoryID;
	}

}
?>